package com.wyh.service.serviceImpl.g3serviceimpl;

import com.wyh.domain.dto.g3dto.CustomerPayDetailDto;
import com.wyh.domain.po.g3po.CustomerBuilding;
import com.wyh.domain.po.g3po.CustomerPayDetail;
import com.wyh.domain.vo.g3vo.CustomerBuildingVo;
import com.wyh.domain.vo.g3vo.CustomerPayDetailVo;
import com.wyh.mapper.g3mapper.CustomerBuildingMapper;
import com.wyh.service.g3service.AsyncService;
import com.wyh.service.g3service.ICustomerBuildingService;
import com.wyh.utils.g3utils.ProjectUtils;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Service
public class CustomerBuildingServiceImpl implements ICustomerBuildingService {
    @Autowired
    private CustomerBuildingMapper customerBuildingMapper;
    @Autowired
    private AsyncService asyncService;
    @Override
    public CustomerBuildingVo selectCustomerBuilding(List<Integer> ids) {
        //创建一个map集合,用于后面封装查询的值
        Map<String, List<CustomerBuilding>> buildMap = new HashMap<>();
        List<CustomerBuilding> builds = customerBuildingMapper.selectBuildingName(ids);
        buildMap.put("builds",builds);
        //将查询到的楼栋id集合传递给下一个查询方法,查询楼层的id和名称
        List<CustomerBuilding> floors = customerBuildingMapper.selectFloorsByBuilding(builds);
        buildMap.put("floors", floors);
        //将查询到的楼层id集合传递给下一个方法,查询客户名称
        List<CustomerBuilding> customers = customerBuildingMapper.selectCustomerName(floors);
        buildMap.put("customers", customers);
        //将三个集合的结果合到一个集合里
        CustomerBuildingVo customerBuildingVo = new CustomerBuildingVo();
        customerBuildingVo.setBuildingMap(buildMap);

        return customerBuildingVo;
    }

    @Override
    public CustomerPayDetailVo selectCustomerPayDetail(CustomerPayDetailDto customerPayDetailDto) {
        CustomerPayDetailVo customerPayDetailVo = new CustomerPayDetailVo();
        Future<List<CustomerPayDetail>> listFuture = asyncService.selectCustomerArrears(customerPayDetailDto);
        List<CustomerPayDetail> customerPayDetails = null;
        try {
            customerPayDetails = listFuture.get(3000L, TimeUnit.MILLISECONDS);
            customerPayDetails.forEach(

                    action -> {
                        Double one = action.getOneToThreeMonthFees() == null ?0:action.getOneToThreeMonthFees();
                        Double four = action.getFourToSixMonthFees() == null ?0:action.getFourToSixMonthFees();
                        Double six = action.getSixMonthFeesAbove() == null ?0:action.getSixMonthFeesAbove();

                        if (action != null) {
                            action.setTotalFees(one + four+ six);
                        }
                    }
            );

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //进行分页处理
        Pair<List<CustomerPayDetail>, Integer> paginate = ProjectUtils.paginate(customerPayDetails, customerPayDetailDto.getCurrentPage(), customerPayDetailDto.getPageSize());
        //封装分页后的数据
        customerPayDetailVo.setTotal(paginate.getValue());
        customerPayDetailVo.setList(paginate.getKey());

        return customerPayDetailVo;
    }
}
